package com.czk.zookeeper.zookeepercli.lock;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/1/5 22:15
 */
public class TicketSeller {
    public void sell() {
        System.out.println("售票开始");
        int sleepMiles = 5000;
        try {
            Thread.sleep(sleepMiles);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        System.out.println(
                "售票结束"
        );
    }

    public void sellTicketWithLock() throws Exception {
        MyLock lock = new MyLock();
        // 尝试获取锁
        lock.acquireLock();
        // 获取锁后售票
        sell();
        // 释放锁
        lock.releaseLock();
    }

    public static void main(String[] args) throws Exception {
        TicketSeller seller = new TicketSeller();
        for (int i = 0; i < 10; i++) {
            seller.sellTicketWithLock();
        }
    }
}
